package ebj.common.annotation.demo_data_provider_manager.data_provider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.Properties;

import org.apache.catalina.Host;
import org.apache.kylin.jdbc.Driver;
import org.junit.Test;

import ebj.common.annotation.demo_data_provider_manager.anno.ProviderName;

@ProviderName("Kylin")
public class KylinDataProvider extends DataProvider {
    String host = "192.168.1.180";
    String user = "KYLIN";
    String pass = "ADMIN";

    @Override
    public String[][] getData(Map<String, String> datasourceConfig,
            Map<String, String> query) throws Exception {
        return null;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return this.getClass().getSimpleName() + "[" + this.host + ", "
                + user + ", "
                + pass + "]";
    }
    
    @Test
    public void testKylinJDBC() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
        String kylin_proj_url = "jdbc:kylin://10.28.66.31:7070/learn_kylin";
        Driver driver = (Driver) Class.forName("org.apache.kylin.jdbc.Driver").newInstance();
        
        Properties info = new Properties();
        info.put("user", "ADMIN");
        info.put("password", "KYLIN");
        Connection conn = driver.connect(kylin_proj_url, info);
        PreparedStatement state = conn.prepareStatement("select part_dt, sum(price) as total_selled, count(distinct seller_id) as sellers from kylin_sales group by part_dt order by part_dt;");
 //     PreparedStatement state = conn.prepareStatement("select * from test_table where id=?");
 //     state.setInt(1, 10);
        ResultSet resultSet = state.executeQuery();
        
        while (resultSet.next()) {
            String result = resultSet.getString("part_dt") + ", " 
                    + resultSet.getBigDecimal("total_selled") + ", " 
                    + resultSet.getInt("sellers");
            System.out.println(result);
        }
    }
}
